package gui.menu.actions;

import engine.Edge;
import engine.Vertex;
import gui.Frame;
import gui.View;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;

import undoredo.AbstractCommand;
import undoredo.DeleteEdgeCommand;
import undoredo.DeleteVertexCommand;

/**
 * Deletes all selected elements.
 */
@SuppressWarnings("serial")
public class DeleteAction extends AbstractAction
{

   private Frame frame;

   /** Create this action for the given frame */
   public DeleteAction(Frame frame)
   {
      super("Delete");
      this.frame = frame;
   }

   @Override
   public void actionPerformed(ActionEvent ae)
   {
      frame.removeListeners();
      View view = frame.getView();
      if (view.getSelectStatu() == true)
      {
         ArrayList<Edge> edge = view.getEdges();
         for (int i = 0; i < edge.size(); i++)
            if (edge.get(i).getStatus() == 1)
            {
               AbstractCommand command = new DeleteEdgeCommand(view,
                     edge.get(i));
               command.execute();
               Frame.addCommandToStack(command);
               Frame.setUndoRedoVisible();
               view.repaint();
            }

         ArrayList<Vertex> vertexs = view.getVertices();
         for (int i = 0; i < vertexs.size(); i++)
            if (vertexs.get(i).getStatus() == 1)
            {
               AbstractCommand command = new DeleteVertexCommand(view,
                     vertexs.get(i));
               command.execute();
               Frame.addCommandToStack(command);
               Frame.setUndoRedoVisible();
               view.repaint();
            }

      }

      // frame.repaint();
   }

}
